package com.curvebreakers.wheresmystuff.activities;

import java.util.List;

import android.app.AlertDialog;
import android.app.Dialog;
import android.app.ListFragment;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.Toast;

import com.curvebreakers.wheresmystuff.R;
import com.curvebreakers.wheresmystuff.model.DBResultListener;
import com.curvebreakers.wheresmystuff.model.DatabaseModel;
import com.curvebreakers.wheresmystuff.model.User;
import com.curvebreakers.wheresmystuff.model.UserAdapter;

public class AdminFragment extends ListFragment implements DBResultListener {
	
	private UserAdapter userAdapter;
	private User currentUser;
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		final View view = inflater.inflate(R.layout.fragment_admin, container, false);
		return view;
	}
	
	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
	    super.onActivityCreated(savedInstanceState);
	    
		DatabaseModel.getAllUsers(this);
		userAdapter = new UserAdapter(getActivity(),android.R.layout.simple_list_item_1);
		setListAdapter(userAdapter);
		
		registerForContextMenu(getListView());
		getListView().setLongClickable(false);
	}
	
	@Override
	public void onListItemClick(ListView l, View v, int position, long id) {
		currentUser = userAdapter.getItem(position);
		l.showContextMenuForChild(v);
	}
	
	/**
	 * Creates a context(popup) menu from the selected user.
	 * Adds a header of the users username.
	 * Checks the user's data and populates the menu with the appropriate options
	 * to promote/demote lock/unlock and delete the user.
	 */
	@Override
	public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
		super.onCreateContextMenu(menu, v, menuInfo);
	    menu.setHeaderTitle(currentUser.getEmail());
	     
		if(!currentUser.isAdmin()) {
			menu.add("Promote User");
		} else {
			menu.add("Demote User");
		}
		
		if(currentUser.isLocked()) {
			menu.add("Unlock User");
		} else {
			menu.add("Lock User");
		}
		
		menu.add("Delete User");
	}


	/**
	 * Sets up the on click responses for the context menu populated above.
	 * On click the user's data is changed and a toast message is displayed for
	 * UI feedback. The list is then recalled from the database with 
	 * the updated information.
	 */
	@Override
	public boolean onContextItemSelected(MenuItem item) {
		super.onContextItemSelected(item);
		if (item.getTitle().equals("Promote User")){
			currentUser.setAdmin(true);
			DatabaseModel.saveUser(currentUser, this);
			Toast.makeText(getActivity(), "Promotion", Toast.LENGTH_SHORT).show();
		} else if (item.getTitle().equals("Demote User")){
			currentUser.setAdmin(false);
			DatabaseModel.saveUser(currentUser, this);
			Toast.makeText(getActivity(), "Demotion", Toast.LENGTH_SHORT).show();
		} else if (item.getTitle().equals("Unlock User")){
			currentUser.setLocked(false);
			DatabaseModel.saveUser(currentUser, this);
			Toast.makeText(getActivity(), "Unlocked", Toast.LENGTH_SHORT).show();
		} else if (item.getTitle().equals("Lock User")){
			currentUser.setLocked(true);
			DatabaseModel.saveUser(currentUser, this);
			Toast.makeText(getActivity(), "Locked", Toast.LENGTH_SHORT).show();
		} else { //Delete
			AlertDialog dialog = new AlertDialog.Builder(getActivity()).create();
			dialog.setTitle("Are you sure you want to delete "+currentUser.getEmail());
			dialog.setButton(Dialog.BUTTON_POSITIVE, "Delete User", new DialogInterface.OnClickListener() {
				@Override
				public void onClick(DialogInterface dialog, int which) {
					DatabaseModel.deleteUser(currentUser, AdminFragment.this);
					userAdapter.remove(currentUser);
					userAdapter.notifyDataSetChanged();
					Toast.makeText(getActivity(), "User Removed", Toast.LENGTH_SHORT).show();
				}
			});
			dialog.setButton(Dialog.BUTTON_NEGATIVE, "Cancel", new DialogInterface.OnClickListener() {
				@Override
				public void onClick(DialogInterface dialog, int which) {
				}
			});
			dialog.show();
		}
		userAdapter.notifyDataSetChanged();
		return true;
	}


	@Override
	@SuppressWarnings("unchecked")
	public void onDBResultReceived(Object obj) {
		if (obj == null)
			return;
		if (!(obj instanceof User)){
			userAdapter.clear();
			userAdapter.addAll((List<User>) obj);
		}
	}

	@Override
	public void onDBRequestFailure(String message) {
		Toast.makeText(getActivity(), message, Toast.LENGTH_LONG).show();
	}
}
